//
//  CRNewfeatureController.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/18.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRNewfeatureController.h"
#import "CRTabBarViewController.h"
#import "CRLoginController.h"
#define NewfeatureCount 4


@interface CRNewfeatureController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation CRNewfeatureController

#pragma mark >>>> 系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 自定义控件初始化
    [self setupSubViews];
    
}

#pragma mark >>>> 初始化方法
/**
 *  自定义控件初始化
 */
- (void)setupSubViews {
    // 1.创建一个scrollView 显示所有的新特性图片
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    // 设置代理对象
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // 2.添加图片到scrollView
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    
    for (int i = 0; i < NewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 如果是最后一个imageView就往里面添加其他内容
        if (i == NewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    // 3. 设置scrollView 的其他属性
    scrollView.contentSize = CGSizeMake(NewfeatureCount * scrollW, scrollH);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO   ; // 去除弹簧效果
    scrollView.showsHorizontalScrollIndicator = NO;
    
    // 4. 添加pageControl 分页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = NewfeatureCount;
    pageControl.centerX = scrollW * 0.5;
    pageControl.y = scrollH - 30;
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.108 green:0.511 blue:1.000 alpha:1.000];
    pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.502 alpha:1.000];
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

#pragma mark >>>>UIScrollViewDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入求计算出页码
    self.pageControl.currentPage = (int) (page + 0.5);
}

#pragma mark >>>> 其他方法
- (void)setupLastImageView:(UIImageView *)imageView {
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    // 1.分享给大家(checkBox)
    UIButton * shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    
    shareBtn.width = 120;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.65;
    
    shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
   // [imageView addSubview:shareBtn];
    
    // 2.开始
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = shareBtn.centerX;
    startBtn.centerY = imageView.height * 0.7;
    [startBtn setTitle:@"开始畅聊" forState:UIControlStateNormal];
    
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview:startBtn];
    
}
#pragma mark >>>> 点击监听事件
- (void)shareClick:(UIButton *)button {
    // 状态取反
    button.selected = !button.selected;
}

- (void)startClick {
    /**
     切换控制器的手段
     1.push:依赖于UINavigationController 控制器的切换是可逆的 比如A切换到B B又可以回到A
     2.modal:控制器的切换是可逆的 比如A切换到B B又可以回到A
     3.切换window的根视图控制器 上一个控制器会被销毁
     */
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[CRLoginController alloc] init];
    
}


@end
